package com.edu.compile.util;

import com.edu.compile.AbstractCompilerParser;
import com.edu.model.dto.NodeMetadata;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class CompilerDispatcher {

    private static Map<String, AbstractCompilerParser> compilerMap;

    @Resource
    public void setCompilerMap(Map<String, AbstractCompilerParser> compilerMap) {
        CompilerDispatcher.compilerMap = compilerMap;
    }

    /**
     * 产生数据包处理接口
     *
     * @param beanName
     * @return
     */
    public static <T> T getHandler(String beanName) {
        return (T) compilerMap.get(beanName);
    }

    public static AbstractCompilerParser getCompiler(NodeMetadata nodeMetadata) {
        return getHandler(getCompilerName(nodeMetadata));
    }

    public static String getCompilerName(NodeMetadata nodeMetadata) {
        return nodeMetadata.getNodeType() + "_compiler";
    }
}
